<?php

/**
 * 后台上传手机图片
 */
class AppController extends AdminBase {

    /**
     * 更新app首页图片
     */
    public function actionStartpic() {
        $model = new AppStartpicModel();
        if (IS_POST) {
            $url = Yii::app()->request->getParam('url');
            $picInfo = $model->getPicInfo();
            $model->setValidators($model->rules);
            // print_r($picInfo);exit;          
            $model->attributes = $picInfo[0];
            $model->url = $url;
            $model->title = Yii::app()->request->getParam('title');
            if ($model->validate() && $model->save()) {
                $this->success('更新成功！', self::U('startpic'));
            } else {
                $this->error(UploadFile::getInstance()->getError() ? UploadFile::getInstance()->getError() : '更新失败,请重新上传！');
            }
        } else {
            $this->render();
        }
    }

    //上传广告图
    public function actionAdpic() {
        $model = new AppStartpicModel();
        if (IS_POST) {
            $url = Yii::app()->request->getParam('url');
            $picInfo = $model->getPicInfo();
            $model->setValidators($model->rules);
            // print_r($picInfo);exit;          
            $model->attributes = $picInfo[0];
            $model->url = $url;
            if ($model->validate() && $model->save()) {
                $this->success('更新成功！', self::U('adpic'));
            } else {
                $this->error(UploadFile::getInstance()->getError() ? UploadFile::getInstance()->getError() : '更新失败,请重新上传！');
            }
        } else {
            $this->render();
        }
    }

    /**
     * 展示上传图片管理
     */
    public function actionDisplay() {
        $this->basePage('AppStartpicModel', array('platform' => array('EGT', 2), 'order' => 'uploadtime desc'), 20);
        $this->render('display');
    }

    //启动图管理
    public function actionQdpic() {
        $this->basePage('AppStartpicModel', array('platform' => array('ELT', 1), 'order' => 'uploadtime desc'), 20);
        $this->render('qdpic');
    }

    //编辑图片选项
    public function actionEditpic() {
        // $this->baseEdit('AppStartpicModel', 'qdpic');
        $attachmentmodel = new AttachmentModel();
        $model = new AppStartpicModel();
        $id = Yii::app()->request->getParam('pid');
        $aid = $aid = Yii::app()->request->getParam('aid');
        $data = $model->findByPk($id);
        if (empty($data)) {
            $this->error('该信息不存在！');
        }
        if (IS_POST) {
            $picInfo = $model->getPicInfo();
            $data->attributes = $_POST;
            if (!empty($picInfo)) {
                $attachmentmodel->deleteFileByAid($aid);
                $data->filepath = $picInfo[0]['filepath'];
                $data->aid = $picInfo[0]['aid'];
            }
            if ($data->validate() && $data->save()) {
                $this->success('修改成功！', self::U('qdpic'));
            } else {
                $error = $data->getOneError();
                $this->error($error ? $error : '修改失败！');
            }
        } else {
            $this->assign('data', $data);
            $this->render();
        }
    }

    /**
     * 删除图片
     */
    public function actionDelete() {
        $model = new AppStartpicModel();
        $aid = Yii::app()->request->getParam('aid');
        if ($model->deletePic($aid)) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败！');
        }
    }

}
